//
//  discountDetailViewController.m
//  Wedding fantastic
//
//  Created by dllo on 15/7/3.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "discountDetailViewController.h"
#import "discountDetailModel.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"

@interface discountDetailViewController ()
// scroll
@property (nonatomic, retain)UIScrollView *scrollview;
// 背景scroll
@property (nonatomic, retain)UIScrollView *backgroundScrollView;
// 存储视图信息
@property (nonatomic, retain)discountDetailModel *info;
// 存储轮播图的数组
@property (nonatomic, retain)NSMutableArray *picArr;
// 标题标签
@property (nonatomic, retain)UILabel *titleLabel;
// 类型标签
@property (nonatomic, retain)UILabel *kindLabel;
// 套餐描述标签
@property (nonatomic, retain)UILabel *describeLabel;
// 套餐描述视图标签
@property (nonatomic, retain)UILabel *describeViewLabel;
// 购买须知标签
@property (nonatomic, retain)UILabel *purchase_notesLabel;
// 购买须知视图标签
@property (nonatomic, retain)UILabel *purchase_notesViewLabel;
@end

@implementation discountDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"套餐";
    self.view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    // bar
    self.navigationController.navigationBar.translucent = NO;
    // 解析数据
    [self createDiscountDetail];
    
    //
    [self createView];
    
}
- (void)dealloc {
    [_ID release];
    [_info release];
    [_scrollview release];
    [_picArr release];
    [_titleLabel release];
    [_describeLabel release];
    [_describeViewLabel release];
    [_purchase_notesLabel release];
    [_purchase_notesViewLabel release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.picArr = [NSMutableArray array];
    }
    return self;
}
//
- (void)createView {
    // 背景scroll
    self.backgroundScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    [self.view addSubview:self.backgroundScrollView];
    self.backgroundScrollView.backgroundColor = [UIColor whiteColor];
    // 设置偏移范围
    self.backgroundScrollView.contentOffset = CGPointMake(0, kHeight);
    // 设置scrollview不按页滚动
    self.backgroundScrollView.pagingEnabled = NO;
    // 设置滚动范围
    self.backgroundScrollView.contentSize = CGSizeMake(0, kHeight * 3);
    
    // 轮播图
    self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 300 * kHeight / 667)];
    [self.backgroundScrollView addSubview:self.scrollview];
    self.scrollview.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    [_scrollview release];
    // 设置偏移范围
    self.scrollview.contentOffset = CGPointMake(kWidth, 0);
    // 设置scrollview按页滚动
    self.scrollview.pagingEnabled = YES;
    
    
    // 标题标签
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 300 * kHeight / 667, kWidth, 40 * kHeight / 667)];
    [self.backgroundScrollView addSubview:self.titleLabel];
    [self.titleLabel release];
    self.titleLabel.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    // 居中显示
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    
    
    // 类型标签
    self.kindLabel = [[UILabel alloc] init];
    
    
    // 套餐描述标签
    self.describeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 245 * kHeight / 667, kWidth, 30 * kHeight / 667)];
    [self.backgroundScrollView addSubview:self.describeLabel];
    self.describeLabel.backgroundColor = [UIColor whiteColor];
    self.describeLabel.font = [UIFont systemFontOfSize:12];
    [self.describeLabel release];
    // 给套餐描述标签赋值
    self.describeLabel.text = @" 📜 套餐描述";
    
    
    // 套餐描述视图标签
    self.describeViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * kWidth / 375, 280 * kHeight / 667, kWidth - 20 * kWidth / 375, 10 * kHeight / 667)];
    [self.backgroundScrollView addSubview:self.describeViewLabel];
    self.backgroundScrollView.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    [self.describeViewLabel release];
    self.describeViewLabel.font = [UIFont systemFontOfSize:14];
    
    
    // 购买须知标签
    self.purchase_notesLabel = [[UILabel alloc] init];
    [self.backgroundScrollView addSubview:self.purchase_notesLabel];
    [self.purchase_notesLabel release];
    self.purchase_notesLabel.font = [UIFont systemFontOfSize:16];
    // 居中
    self.purchase_notesLabel.textAlignment = NSTextAlignmentCenter;
    self.purchase_notesLabel.backgroundColor = [UIColor whiteColor];
    
    // 购买须知视图标签
    self.purchase_notesViewLabel = [[UILabel alloc] init];
    [self.backgroundScrollView addSubview:self.purchase_notesViewLabel];
    self.purchase_notesViewLabel.font = [UIFont systemFontOfSize:14];
    [self.purchase_notesViewLabel release];
    
    // 重新设置scroll高度
    
    // 回到顶部button
    UIButton *topButton = [UIButton buttonWithType:UIButtonTypeCustom];
    topButton.frame = CGRectMake(300 * kWidth / 375, 450 * kHeight / 667, 35 * kWidth / 375, 38 * kHeight / 667);
    [topButton setBackgroundImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateNormal];
    topButton.alpha = 0.6;
    [self.view addSubview:topButton];
    [topButton addTarget:self action:@selector(topAction:) forControlEvents:UIControlEventTouchUpInside];
}
// 回到顶部
- (void)topAction:(UIButton *)button {
    [self.backgroundScrollView setContentOffset:CGPointZero animated:YES];
}
// 解析超值折扣详情页面(案例)的信息
- (void)createDiscountDetail {
    NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/v2/api/app/works/%@.json", self.ID];
    // 1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    // 2.让管理类调用get请求
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果成功返回数据, 接收数据
        NSDictionary *resultDic = responseObject;
        NSDictionary *workDic = resultDic[@"work"];
        self.info = [[discountDetailModel alloc] init];
        [self.info setValuesForKeysWithDictionary:workDic];
        // 给轮播图的数组里添加数据
        NSArray *media_itemsArr = self.info.media_items;
        if (media_itemsArr.count > 1) {
            // 先把最后一个图片添加到数组中
            [self.picArr addObject:media_itemsArr[media_itemsArr.count - 1][@"media_path"]];
            
            for (NSDictionary *tempDic in media_itemsArr) {
                [self.picArr addObject:tempDic[@"media_path"]];
            }
            // 最后把第一个图片添加到数组的最后
            [self.picArr addObject:self.picArr[1]];
            // 给轮播图赋值
            // 设置滚动范围
            self.scrollview.contentSize = CGSizeMake(kWidth * self.picArr.count, 0);
            
            
        } else {
            for (NSDictionary *tempDic in media_itemsArr) {
                [self.picArr addObject:tempDic[@"media_path"]];
            }
            // 设置滚动范围
            self.scrollview.contentSize = CGSizeMake(kWidth * self.picArr.count, 0);
        }
        // 把图片加到scrollview上
        for (NSInteger i = 0; i < self.picArr.count; i++) {
            UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i + 10 * kWidth / 375, 10 * kHeight / 667, kWidth - 20 * kWidth / 375, self.scrollview.frame.size.height - 20 * kHeight / 667)];
            [self.scrollview addSubview:imageview];
            [imageview sd_setImageWithURL:[NSURL URLWithString:self.picArr[i]]];
            [imageview release];
        }
        
        // 给标题标签赋值
        self.titleLabel.text = self.info.title;
        // 文字自适应
        [self wordsFit:self.titleLabel];
        self.titleLabel.frame = CGRectMake(0, self.scrollview.frame.size.height, kWidth, self.titleLabel.frame.size.height);
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        
        // 套餐描述(重新设置位置)
        self.describeLabel.frame = CGRectMake(0, self.titleLabel.frame.size.height + 300 * kHeight / 667, kWidth, self.describeLabel.frame.size.height);
        
        // 给描述视图标签赋值
        self.describeViewLabel.text = self.info.describe;
        // 文字自适应
        [self wordsFit:self.describeViewLabel];
        self.describeViewLabel.frame = CGRectMake(10 * kWidth / 375, self.titleLabel.frame.size.height + 300 * kHeight / 667 + self.describeLabel.frame.size.height, kWidth - 20 * kWidth / 375, self.describeViewLabel.frame.size.height);
        
        // 购买须知标签
        NSString *purchase_notesLabelStr = [NSString stringWithFormat:@"-----购买须知-----"];
        self.purchase_notesLabel.text = purchase_notesLabelStr;
        self.purchase_notesLabel.frame = CGRectMake(0, self.titleLabel.frame.size.height + 300 * kHeight / 667 + self.describeLabel.frame.size.height + self.describeViewLabel.frame.size.height, kWidth, 30 * kHeight / 667);
        
        // 购买须知视图标签
        self.purchase_notesViewLabel.text = self.info.purchase_notes;
        // 文字自适应
        [self wordsFit:self.purchase_notesViewLabel];
        self.purchase_notesViewLabel.frame = CGRectMake(10 * kWidth / 375, self.titleLabel.frame.size.height + 300 * kHeight / 667 + self.describeLabel.frame.size.height + self.describeViewLabel.frame.size.height + self.purchase_notesLabel.frame.size.height, kWidth - 20 * kWidth / 375, self.purchase_notesViewLabel.frame.size.height);
        
//        NSLog(@"%@", self.ID);
        
        // 重新设置背景scroll高度
        self.backgroundScrollView.frame = CGRectMake(0, 0, self.backgroundScrollView.frame.size.width, self.scrollview.frame.size.height + self.titleLabel.frame.size.height + self.describeLabel.frame.size.height + self.describeViewLabel.frame.size.height + self.purchase_notesLabel.frame.size.height + self.purchase_notesViewLabel.frame.size.height);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 如果失败,返回错误信息
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"悲催,解析数据失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
    }];
    
    

}
// 文字自适应
- (void)wordsFit :(UILabel *)label{
    // 文字自适应
    // 计算文本的空间,MAXFLOAT为无限大
    CGRect rect = [label.text boundingRectWithSize:CGSizeMake(kWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: label.font} context:nil];
    // 设置UILabel的最大显示行数(=0时不限制)
    label.numberOfLines = 0;
    // 用计算出来的GCSize/GCRect设置UILabel的frame
    label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, rect.size.width, rect.size.height);
    label.frame= CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
